﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace YinLongTool.Number
{
    public static class IntUtil
    {
        public static int Parse( string intstirng, int defaultvalue = 0)
        {
            int result = defaultvalue;
            try
            {
                if (int.TryParse(intstirng, out result))
                {
                    return Convert.ToInt32(intstirng);
                }
                return defaultvalue;
            }
            catch
            {
                return defaultvalue;
            }
        }
        /// <summary>
        /// 将double转换成int
        /// </summary>
        /// <param name="num">double类型</param>
        /// <returns>int类型</returns>
        public static int ToInt32(double num)
        {
            return (int)Math.Floor(num);
        }

        /// <summary>
        /// 将double转换成int
        /// </summary>
        /// <param name="num">double类型</param>
        /// <returns>int类型</returns>
        public static int ToInt32(decimal num)
        {
            return (int)Math.Floor(num);
        }
        /// <summary>
        /// 转换成字节数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] GetBytes( int value)
        {
            return BitConverter.GetBytes(value);
        }

      

        /// <summary>
        /// 字符串转int
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns>int类型的数字</returns>
        public static int ToInt32(string s)
        {
            Int32.TryParse(s, out int result);
            return result;
        }
        /// <summary>
        /// 字符串转int
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns>int类型的数字</returns>
        public static int ToInt32(string s, int defaultValue = 0)
        {
            return int.TryParse(s, out int result) ? result : defaultValue;
        }
     
    }
}
